<?php

class UserController extends Blog_Controller_AdminController {

	public function indexAction() {
		// action body
	}

	public function addAction() {
		$Form = new Zend_Form(array('name' => 'AddUserForm'));
		$Form->setMethod(Zend_Form::METHOD_POST)
				->setDecorators(array(array('ViewScript', array('viewScript' => '/user/_createUserForm.phtml'))))
				->setAttrib('autocomplete', 'off')
				->setEnctype(Zend_Form::ENCTYPE_MULTIPART)
				->addElements(array(
					new Zend_Form_Element_Text('UserName', array('placeholder' => 'Tên đăng nhập', 'style' => 'width: 100%;', 'required' => TRUE,
						'validators' => array(array('StringLength', FALSE, array(3, 50)), array('Alnum', FALSE, FALSE)))),
					new Zend_Form_Element_Password('Password', array('placeholder' => 'Mật khẩu', 'style' => 'width: 100%;', 'required' => TRUE, 'validators' => array(array('StringLength', FALSE, array(8, 32))))),
					new Zend_Form_Element_Password('PasswordConfirmation', array('placeholder' => 'Xác nhận mật khẩu', 'style' => 'width: 100%;', 'required' => TRUE)),
					new Zend_Form_Element_Text('Email', array('placeholder' => 'Địa chỉ email', 'style' => 'width: 100%;', 'required' => TRUE)),
					new Zend_Form_Element_File('Image', array('validators' => array(array('Extension', FALSE, 'png, jpg')))),
					new Zend_Form_Element_Submit('save', 'Lưu')
				));
		
		if ($this->getRequest()->isPost()) {
			$IsValid = $Form->isValid($this->getRequest()->getPost());
			
			if ($Form->getValue('Password') != $Form->getValue('PasswordConfirmation')) {
				$IsValid = FALSE;
				$Form->getElement('PasswordConfirmation')->addError('Mật khẩu và Xác nhận mật khẩu không giống nhau');
			}
			
			if ($IsValid) {
				$a = new Zend_File_Transfer_Adapter_Http();
				
				$a->setDestination(realpath(APPLICATION_PATH . '/../data/user/img/'))->receive();
				$Form->getElement('Image')->addFilter();
				$User = new Admin_Model_User_User();
				$User->setUserName($Form->getValue('UserName'))
						->setPassword(Zend_Crypt::hash('SHA1', $Form->getValue('Password')))
						->setCreated(time())
						->setStatus(1)
						->setGroupId(NULL)
						->setMeta('')
						->setId(NULL)
						->setMeta(Zend_Serializer::serialize(array('Image' => $a->getFileName('Image', FALSE))))
						->setEmail($Form->getValue('Email'));
						
				$Mapper = new Admin_Model_User_Mapper('Db');

				try {
					if ($Mapper->save($User)) {
						$Form->reset();
					}
				} catch (Exception $e) {
					switch ($e->getCode()) {
						case 23000:
							$Message = Blog_Message_Sql::getInstance()->setMessage($e->getMessage())->getMessage();
							break;
					}
					
					$this->view->assign('Message', '<div class="message ui-state-error">' . $Message . '</div>');
				}

				unset($User, $Mapper);
			} else {
				$Messages = '<ul class="message error">';
				
				foreach (Blog_Message_Form::getInstance()->setForm($Form)->getMessages() as $Message) {
					$Messages .= '<li>' . $Message . '</li>' . PHP_EOL;
				}
				$Messages .= '</ul>';
				
				$this->view->assign('Message', $Messages);
			}
		}

		$this->view->assign('AddUserForm', $Form);
	}

	public function listAction() {
		$Mapper = new Admin_Model_User_Mapper('Db');
		$Paginator = $Mapper->fetchPagination(null, 'UserName');
		$Paginator->setCurrentPageNumber((int) $this->getRequest()->getParam('page'))->setItemCountPerPage(20)->setPageRange(5);
		$Users = $Paginator->getCurrentItems();
		
		$From = $Paginator->getItemCountPerPage() * ($Paginator->getCurrentPageNumber() - 1) + 1;
		$To = min($From + $Paginator->getItemCountPerPage(), $Paginator->getTotalItemCount());
		$this->view->assign('From', $From);
		$this->view->assign('To', $To);
		$this->view->assign('Total', $Paginator->getTotalItemCount());
		$this->view->assign('Users', $Users);
		$this->view->assign('Paginator', (string) $Paginator->render());
	}

}